//                        date: [[20120731120158]]
//                        Author: Ahmed Fathi Hamouda Abd El Hafez
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using AdoHelper;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using System.Data.Common;


namespace CustomersSuppliersDataLayer
{

    /// <summary>
    /// data layer class OF Table Images.
    /// </summary>
    [Serializable]
    public partial class DLImage
    {

        #region Fields
        private Guid? f_Image_ID = null;
        private byte[] f_Image_Default = null;
        private byte[] f_Image_Data = null;
        private string f_Image_Description = null;
        private byte[] f_Image_TimeStamp = null;

        //private Dictionary<string, ColumnMetaData> metaData = new Dictionary<string, ColumnMetaData>();

        private SelectBuilder f_SelectBuilder = new SelectBuilder();
        private UpdateBuilder f_UpdateBuilder = new UpdateBuilder();
        private DeleteBuilder f_DeleteBuilder = new DeleteBuilder();
        private InsertBuilder f_InsertBuilder = new InsertBuilder();

        private Exception f_Error = null;
        private List<DataFilter> f_Filters = new List<DataFilter>();
        private List<DataSort> f_Sorts = new List<DataSort>();
        #endregion

        #region Properties
        /// <summary>.
        /// Gets or sets the Image_ID value.
        /// </summary>
        public Guid? Image_ID
        {
            get
            {
                return this.f_Image_ID;
            }
            set
            {
                this.f_Image_ID = value;
            }
        }
        /// <summary>.
        /// Gets or sets the Image_Default value.
        /// </summary>
        public byte[] Image_Default
        {
            get
            {
                return this.f_Image_Default;
            }
            set
            {
                this.f_Image_Default = value;
            }
        }
        /// <summary>.
        /// Gets or sets the Image_Data value.
        /// </summary>
        public byte[] Image_Data
        {
            get
            {
                return this.f_Image_Data;
            }
            set
            {
                this.f_Image_Data = value;
            }
        }
        /// <summary>.
        /// Gets or sets the Image_Description value.
        /// </summary>
        public string Image_Description
        {
            get
            {
                return this.f_Image_Description;
            }
            set
            {
                this.f_Image_Description = value;
            }
        }
        /// <summary>.
        /// Gets or sets the Image_TimeStamp value.
        /// </summary>
        public byte[] Image_TimeStamp
        {
            get
            {
                return this.f_Image_TimeStamp;
            }
            set
            {
                this.f_Image_TimeStamp = value;
            }
        }

        /// <summary>
        /// Update Query Builders 
        /// </summary>
        public UpdateBuilder UpdateBuilder
        {
            get { return f_UpdateBuilder; }
        }
        /// <summary>
        /// select Query Builders 
        /// </summary>
        public SelectBuilder SelectBuilder
        {
            get { return f_SelectBuilder; }
        }
        /// <summary>
        /// Delete Query Builders 
        /// </summary>
        public DeleteBuilder DeleteBuilder
        {
            get { return f_DeleteBuilder; }
        }
        /// <summary>
        /// InsertValues Query Builders
        /// </summary>
        public InsertBuilder InsertBuilder
        {
            get { return f_InsertBuilder; }
        }
        /// <summary>
        /// get the error ocured after insert or update or delete or load
        /// </summary>

        public Exception Error
        {
            get { return this.f_Error; }
        }
        /// <summary>
        /// return filters
        /// </summary>
        public List<DataFilter> Filters
        {
            get { return f_Filters; }
        }
        /// <summary>
        /// return load sorts
        /// </summary>
        public List<DataSort> Sorts
        {
            get { return f_Sorts; }
        }

        /// <summary>
        /// get the iDataBase Object of dataLayer
        /// </summary>
        public static AdoHelper.IDataBase CustomersSuppliers
        {
            get
            {
                return CustomersSuppliersDataLayer.DataBaseConnections.CustomersSuppliers == null ? new AdoHelper.DataBase() : CustomersSuppliersDataLayer.DataBaseConnections.CustomersSuppliers;
            }
        }

        /// <summary>
        /// get the tablename corresponds to DLImage
        /// </summary>
        public static string TableName
        {
            get
            {
                return "Images";
            }
        }
        /// <summary>
        /// get the Schema corresponds to DLImage
        /// </summary>
        public static string Schema
        {
            get
            {
                return "dbo";
            }
        }
        /// <summary>
        /// get the tableFullName corresponds to DLImage
        /// </summary>
        public static string TableFullName
        {
            get
            {
                return CustomersSuppliers.IdentifierPrefix + Schema + CustomersSuppliers.IdentifierPostfix
                        + "." + CustomersSuppliers.IdentifierPrefix + TableName + CustomersSuppliers.IdentifierPostfix;
            }
        }

        ///// <summary>
        ///// get the metaData for the columns in Images table.
        ///// </summary>
        //public Dictionary<string, ColumnMetaData> MetaData
        //{
        //    get { return metaData; }
        //}
        #endregion

        #region Constructors

        public DLImage()
        {
            this.f_InsertBuilder.TableName = DLImage.TableFullName;
            this.f_UpdateBuilder.TableName = DLImage.TableFullName;

            this.f_DeleteBuilder.TableName = DLImage.TableFullName;
            this.f_SelectBuilder.Select.Add(DLImage.TableFullName + ".*");
            this.f_SelectBuilder.From.InnerJoin(DLImage.TableFullName);


            this.f_Image_ID =  Guid.NewGuid();

        }

        public DLImage(DataRow dr)
            : this()
        {
            this.ImportFrom(dr);
        }

        public DLImage(DbDataReader reader)
            : this()
        {
            this.ImportFrom(reader);
        }

        #endregion

        #region Methods

        private string addPrefix(string parameterName)
        {
            return CustomersSuppliers.ParameterPrefix + parameterName;
        }
        private void FillFilter(WhereClause whereClause, List<DbParameter> list)
        {
            for (int i = 0; i < this.f_Filters.Count; i++)
            {
                string expression = "";
                DbParameter parameterNumber = this.IntializaParameter("FilterPatmeter_" + i, this.Filters[i].Value, ParameterDirection.Input,this.Filters[i].CsType);
                if (list != null)
                {
                    list.Add(parameterNumber);
                }
                if (this.f_Filters[i].FilterOperator == FilterOperators.BeginsWith)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " LIKE " + parameterNumber.ParameterName + " +'%' ";
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.Contains)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " LIKE '%'+" + parameterNumber.ParameterName + " +'%' ";
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.EndsWith)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " LIKE '%'+" + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.Equal)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " = " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.NotEqual)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " <> " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.GreaterThan)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " > " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.GreaterThanOrEqual)
                {
                    expression = this.f_Filters[i].Property + " >= " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.LessThan)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " < " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.LessThanOrEqual)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " <= " + parameterNumber.ParameterName;
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.Undefined)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " IS NULL ";
                }
                else if (this.f_Filters[i].FilterOperator == FilterOperators.IN)
                {
                    expression = DLImage.PropertiesMappings.getColumnName(this.f_Filters[i].Property) + " IN (" + this.f_Filters[i].Value + " )";
                }
                if (expression != "")
                {
                    if (this.f_Filters[i].LogicalOperator == LogicalOperators.And)
                    {
                        whereClause.AND(expression);
                    }
                    else if (this.f_Filters[i].LogicalOperator == LogicalOperators.AndNOt)
                    {
                        whereClause.ANDNot(expression);
                    }
                    else if (this.f_Filters[i].LogicalOperator == LogicalOperators.OR)
                    {
                        whereClause.OR(expression);
                    }
                    else if (this.f_Filters[i].LogicalOperator == LogicalOperators.ORNOT)
                    {
                        whereClause.ORNot(expression);
                    }
                }
            }
        }
        private void FillSort(List<DbParameter> list)
        {
            this.f_SelectBuilder.OrderBy.Clear();
            for (int i = 0; i < f_Sorts.Count; i++)
            {
                string expression = DLImage.PropertiesMappings.getColumnName(this.f_Sorts[i].Property) + (this.f_Sorts[i].Descending ? " DESC " : " ");
                this.f_SelectBuilder.OrderBy.Add(expression);
            }
        }
        private DbType convertCSharpTypeToDbType(Type t)
        {
            DbType dbType= DbType.String ;
            if (t.FullName.ToLower().Contains("System.Boolean".ToLower()))
            {
                dbType = DbType.Boolean;
            }
            //must before byte
            else if (t.FullName.ToLower().Contains("System.Byte[]".ToLower()))
            {
                dbType = DbType.Binary;
            }
            else if (t.FullName.ToLower().Contains("System.Byte".ToLower()))
            {
                dbType = DbType.Byte;
            }
            else if (t.FullName.ToLower().Contains("System.SByte".ToLower()))
            {
                dbType = DbType.SByte;
            }
            else if (t.FullName.ToLower().Contains("System.Int16".ToLower()))
            {
                dbType = DbType.Int16;
            }
            else if (t.FullName.ToLower().Contains("System.Int32".ToLower()))
            {
                dbType = DbType.Int32;
            }
            else if (t.FullName.ToLower().Contains("System.Int64".ToLower()))
            {
                dbType = DbType.Int64;
            }
            else if (t.FullName.ToLower().Contains("System.UInt16".ToLower()))
            {
                dbType = DbType.UInt16;
            }
            else if (t.FullName.ToLower().Contains("System.UInt32".ToLower()))
            {
                dbType = DbType.UInt32;
            }
            else if (t.FullName.ToLower().Contains("System.UInt64".ToLower()))
            {
                dbType = DbType.UInt64;
            }

            else if (t.FullName.ToLower().Contains("System.Single".ToLower()))
            {
                dbType = DbType.Single;
            }
            else if (t.FullName.ToLower().Contains("System.Double".ToLower()))
            {
                dbType = DbType.Double;
            }
            else if (t.FullName.ToLower().Contains("System.Decimal".ToLower()))
            {
                dbType = DbType.Decimal;
            }
            else if (t.FullName.ToLower().Contains("System.TimeSpan".ToLower()))
            {
                dbType = DbType.Time;
            }
            else if (t.FullName.ToLower().Contains("System.DateTime".ToLower()))
            {
                dbType = DbType.DateTime;
            }
            else if (t.FullName.ToLower().Contains("System.DateTimeOffset".ToLower()))
            {
                dbType = DbType.DateTimeOffset;
            }
            else if (t.FullName.ToLower().Contains("System.Char".ToLower()))
            {
                dbType = DbType.String;
            }
            else if (t.FullName.ToLower().Contains("System.String".ToLower()))
            {
                dbType = DbType.String;
            }
            else if (t.FullName.ToLower().Contains("System.Guid".ToLower()))
            {
                dbType = DbType.Guid;
            }
            return dbType;
        }


        /// <summary>
        /// initialize parmeter from the factory
        /// </summary>
        public DbParameter IntializaParameter(string nameWithoutPrefix, object value, ParameterDirection direction,Type t)
        {
            DbParameter parameter = CustomersSuppliers.Factory.CreateParameter();
            parameter.ParameterName = this.addPrefix(nameWithoutPrefix);
            parameter.Value = value == null ? DBNull.Value : value;
            parameter.Direction = direction;
            parameter.DbType = convertCSharpTypeToDbType(t);
            return parameter;
        }


        /// <summary>
        /// insert a record to the Images table.
        /// </summary>
        public int Insert(string[] properties, string[] autoDateProperties)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);
                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);
                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);
                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);
                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);

                this.f_InsertBuilder.Insert.Clear();
                this.f_InsertBuilder.Values.Clear();
                for (int i = 0; i < properties.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), this.addPrefix(properties[i])));
                }
                for (int i = 0; i < autoDateProperties.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), CustomersSuppliers.SystemDateFunction));
                }
                string query = "\r\n" + this.f_InsertBuilder.Statement;
                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                sb.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Default + " = " + this.addPrefix("Image_Default"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Data + " = " + this.addPrefix("Image_Data"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Description + " = " + this.addPrefix("Image_Description"));
                query += "\r\n" + sb.Statement;


                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, ref this.f_Error, list.ToArray());
                if (this.f_Error == null)
                {
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }


        /// <summary>
        /// insert a record to the Images table using settings.
        /// </summary>
        public int Insert(string[] properties, string[] autoDateProperties, Settings setting)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);
                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);
                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);
                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);
                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);
                list.AddRange(setting.Parameters);

                this.f_InsertBuilder.Insert.Clear();
                this.f_InsertBuilder.Values.Clear();
                for (int i = 0; i < properties.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), this.addPrefix(properties[i])));
                }
                for (int i = 0; i < autoDateProperties.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), CustomersSuppliers.SystemDateFunction));
                }
                string query = "\r\n" + this.f_InsertBuilder.Statement;
                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                sb.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Default + " = " + this.addPrefix("Image_Default"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Data + " = " + this.addPrefix("Image_Data"));
                
                sb.Where.AND(DLImage.PropertiesMappings.Image_Description + " = " + this.addPrefix("Image_Description"));
                query += "\r\n" + sb.Statement;


                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray());
                if (this.f_Error == null)
                {
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }


        /// <summary>
        /// modify  record in the Images table.
        /// </summary>
        public int Update(string[] properties, string[] autoDateProperties)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);
                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);
                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);
                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);
                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);
                DbParameter Prm_CurrentTimeStamp = this.IntializaParameter("CurrentTimeStamp", DBNull.Value, ParameterDirection.InputOutput,typeof(byte []));
                list.Add(Prm_CurrentTimeStamp);

                string query = "";
                if (this.f_Image_TimeStamp != null)
                {
                    SelectBuilder timeStampSelect = new SelectBuilder();
                    timeStampSelect.Select.Add(this.addPrefix("CurrentTimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                    timeStampSelect.From.CrossJoin(DLImage.TableFullName);
                    timeStampSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                    query += "\r\n" + timeStampSelect.Statement;
                    query += "\r\nIF " + this.addPrefix("CurrentTimeStamp") + " = " + this.addPrefix("Image_TimeStamp");
                    query += "\r\nBEGIN";
                }
                this.f_UpdateBuilder.Update.Clear();
                for (int i = 0; i < properties.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), this.addPrefix(properties[i])));
                }
                for (int i = 0; i < autoDateProperties.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(properties[i], CustomersSuppliers.SystemDateFunction));
                }
                this.FillFilter(this.UpdateBuilder.Where, list);
                query += "\r\n" + this.f_UpdateBuilder.Statement;

                if (this.f_Image_TimeStamp != null)
                {
                    query += "\r\nEND";
                }
                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Top = 1;
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                this.FillFilter(sb.Where, null);
                query += "\r\n" + sb.Statement;


                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, ref this.f_Error, list.ToArray());
                if (this.f_Image_TimeStamp != null && Prm_CurrentTimeStamp.Value != DBNull.Value)
                {
                    byte[] timeStampOldvalues = (byte[])f_Image_TimeStamp;
                    byte[] timeStampNewValues = (byte[])Prm_CurrentTimeStamp.Value; ;
                    for (int i = 0; i < timeStampOldvalues.Length; i++)
                    {
                        if (timeStampOldvalues[i] != timeStampNewValues[i])
                        {
                            this.f_Error = new Exception("Concurrency Problem,TimeStamp Value Changed.");
                            break;
                        }
                    }
                }
                if (this.f_Error == null)
                {
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }


        /// <summary>
        /// modify  record in the Images table Using Setings.
        /// </summary>
        public int Update(string[] properties, string[] autoDateProperties, Settings setting)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);
                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);
                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);
                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);
                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);
                DbParameter Prm_CurrentTimeStamp = this.IntializaParameter("CurrentTimeStamp", DBNull.Value, ParameterDirection.InputOutput,typeof(byte[]));
                list.Add(Prm_CurrentTimeStamp);
                list.AddRange(setting.Parameters);

                string query = "";
                if (this.f_Image_TimeStamp != null)
                {
                    SelectBuilder timeStampSelect = new SelectBuilder();
                    timeStampSelect.Select.Add(this.addPrefix("CurrentTimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                    timeStampSelect.From.CrossJoin(DLImage.TableFullName);
                    timeStampSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                    query += "\r\n" + timeStampSelect.Statement;
                    query += "\r\nIF " + this.addPrefix("CurrentTimeStamp") + " = " + this.addPrefix("Image_TimeStamp");
                    query += "\r\nBEGIN";
                }
                this.f_UpdateBuilder.Update.Clear();
                for (int i = 0; i < properties.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), this.addPrefix(properties[i])));
                }
                for (int i = 0; i < autoDateProperties.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(properties[i]), CustomersSuppliers.SystemDateFunction));
                }
                this.FillFilter(this.UpdateBuilder.Where, list);
                query += "\r\n" + this.f_UpdateBuilder.Statement;

                if (this.f_Image_TimeStamp != null)
                {
                    query += "\r\nEND";
                }
                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Top = 1;
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                this.FillFilter(sb.Where, null);
                query += "\r\n" + sb.Statement;


                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray());
                if (this.f_Image_TimeStamp != null && Prm_CurrentTimeStamp.Value != DBNull.Value)
                {
                    byte[] timeStampOldvalues = (byte[])f_Image_TimeStamp;
                    byte[] timeStampNewValues = (byte[])Prm_CurrentTimeStamp.Value; ;
                    for (int i = 0; i < timeStampOldvalues.Length; i++)
                    {
                        if (timeStampOldvalues[i] != timeStampNewValues[i])
                        {
                            this.f_Error = new Exception("Concurrency Problem,TimeStamp Value Changed.");
                            break;
                        }
                    }
                }
                if (this.f_Error == null)
                {
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }


        /// <summary>
        /// delete  records from the Images table.
        /// </summary>
        public int Delete()
        {
            int count = 0;
            this.f_Error = null;
            //this.isDataBaseLoaded = false;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                this.FillFilter(this.DeleteBuilder.Where, list);

                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(this.f_DeleteBuilder.Statement, CommandType.Text, ref this.f_Error, list.ToArray());
            }
            return count;
        }


        /// <summary>
        /// delete  records from the Images table Using settings.
        /// </summary>
        public int Delete(Settings setting)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                list.AddRange(setting.Parameters);
                this.FillFilter(this.DeleteBuilder.Where, list);

                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(this.f_DeleteBuilder.Statement, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray());
            }
            return count;
        }


        /// <summary>
        /// insert a record to the Images table OR Update  it if exists.
        /// </summary>
        public int Insert_Update(string[] propertiesInsert, string[] propertiesUpdate, string[] autoDateInsert, string[] autoDateUpdate, ref bool Inserted)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);
                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);
                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);
                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);
                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);
                DbParameter Prm_CurrentTimeStamp = this.IntializaParameter("CurrentTimeStamp", DBNull.Value, ParameterDirection.InputOutput,typeof(byte[]));
                list.Add(Prm_CurrentTimeStamp);
                DbParameter InsertedParameter = this.IntializaParameter("Inserted", false, ParameterDirection.InputOutput,typeof(bool));
                list.Add(InsertedParameter);


                this.f_InsertBuilder.Insert.Clear();
                this.f_InsertBuilder.Values.Clear();
                this.f_UpdateBuilder.Update.Clear();
                for (int i = 0; i < propertiesInsert.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(propertiesInsert[i]), this.addPrefix(propertiesInsert[i])));
                }
                for (int i = 0; i < propertiesUpdate.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(propertiesUpdate[i]), this.addPrefix(propertiesUpdate[i])));
                }
                for (int i = 0; i < autoDateInsert.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(autoDateInsert[i]), CustomersSuppliers.SystemDateFunction));
                }
                for (int i = 0; i < autoDateUpdate.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(autoDateUpdate[i]), CustomersSuppliers.SystemDateFunction));
                }


                string query = "";
                SelectBuilder existanceSelect = new SelectBuilder();
                existanceSelect.Select.Add("*");
                existanceSelect.From.CrossJoin(DLImage.TableFullName);
                existanceSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                query += "IF NOT EXISTS (" + existanceSelect.Statement.Trim().Replace("\n", "\n		") + ")";
                query += "\r\nBEGIN";
                query += "\r\n	" + this.f_InsertBuilder.Statement.Trim().Replace("\n", "\n	");
                query += "\r\n SET @Inserted=1 ";
                query += "\r\nEND";
                query += "\r\nELSE";
                query += "\r\nBEGIN";

                if (this.f_Image_TimeStamp != null)
                {
                    SelectBuilder timeStampSelect = new SelectBuilder();
                    timeStampSelect.Select.Add(this.addPrefix("CurrentTimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                    timeStampSelect.From.CrossJoin(DLImage.TableFullName);
                    timeStampSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                    query += "\r\n" + timeStampSelect.Statement;
                    query += "\r\nIF " + this.addPrefix("CurrentTimeStamp") + " = " + this.addPrefix("Image_TimeStamp");
                    query += "\r\nBEGIN";
                }
                this.FillFilter(this.f_UpdateBuilder.Where, list);
                query += "\r\n" + this.f_UpdateBuilder.Statement;
                query += "\r\n SET @Inserted=0 ";
                if (this.f_Image_TimeStamp != null)
                {
                    query += "\r\nEND";
                }
                query += "\r\nEND";


                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Top = 1;
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                this.FillFilter(sb.Where, null);
                query += "\r\n" + sb.Statement;


                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, ref this.f_Error, list.ToArray());
                if (this.f_Image_TimeStamp != null && Prm_CurrentTimeStamp.Value != DBNull.Value)
                {
                    byte[] timeStampOldvalues = (byte[])f_Image_TimeStamp;
                    byte[] timeStampNewValues = (byte[])Prm_CurrentTimeStamp.Value; ;
                    for (int i = 0; i < timeStampOldvalues.Length; i++)
                    {
                        if (timeStampOldvalues[i] != timeStampNewValues[i])
                        {
                            this.f_Error = new Exception("Concurrency Problem,TimeStamp Value Changed.");
                            break;
                        }
                    }
                }
                if (this.f_Error == null)
                {
                    Inserted = Convert.ToBoolean(InsertedParameter.Value);
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }

        /// <summary>
        /// insert a record to the Images table OR Update  it if exists using settings.
        /// </summary>
        public int Insert_Update(string[] propertiesInsert, string[] propertiesUpdate, string[] autoDateInsert, string[] autoDateUpdate, ref bool Inserted, Settings setting)
        {
            int count = 0;
            this.f_Error = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                DbParameter Prm_Image_ID = this.IntializaParameter("Image_ID", this.f_Image_ID, ParameterDirection.Input, typeof(Guid));
                list.Add(Prm_Image_ID);

                DbParameter Prm_Image_Default = this.IntializaParameter("Image_Default", this.f_Image_Default, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Default);

                DbParameter Prm_Image_Data = this.IntializaParameter("Image_Data", this.f_Image_Data, ParameterDirection.Input, typeof(byte[]));
                list.Add(Prm_Image_Data);

                DbParameter Prm_Image_Description = this.IntializaParameter("Image_Description", this.f_Image_Description, ParameterDirection.Input, typeof(string));
                list.Add(Prm_Image_Description);

                DbParameter Prm_Image_TimeStamp = this.IntializaParameter("Image_TimeStamp", this.f_Image_TimeStamp, ParameterDirection.InputOutput, typeof(byte[]));
                list.Add(Prm_Image_TimeStamp);
                DbParameter Prm_CurrentTimeStamp = this.IntializaParameter("CurrentTimeStamp", DBNull.Value, ParameterDirection.InputOutput,typeof(byte[]));
                list.Add(Prm_CurrentTimeStamp);
                DbParameter InsertedParameter = this.IntializaParameter("Inserted", false, ParameterDirection.InputOutput,typeof(bool));
                list.Add(InsertedParameter);
                list.AddRange(setting.Parameters);

                this.f_InsertBuilder.Insert.Clear();
                this.f_InsertBuilder.Values.Clear();
                this.f_UpdateBuilder.Update.Clear();
                for (int i = 0; i < propertiesInsert.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(propertiesInsert[i]), this.addPrefix(propertiesInsert[i])));
                }
                for (int i = 0; i < propertiesUpdate.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(propertiesUpdate[i]), this.addPrefix(propertiesUpdate[i])));
                }
                for (int i = 0; i < autoDateInsert.Length; i++)
                {
                    this.f_InsertBuilder.AddcolumnsValues(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(autoDateInsert[i]), CustomersSuppliers.SystemDateFunction));
                }
                for (int i = 0; i < autoDateUpdate.Length; i++)
                {
                    this.f_UpdateBuilder.Update.Add(new KeyValuePair<string, string>(DLImage.PropertiesMappings.getColumnName(autoDateUpdate[i]), CustomersSuppliers.SystemDateFunction));
                }

                string query = "";
                SelectBuilder existanceSelect = new SelectBuilder();
                existanceSelect.Select.Add("*");
                existanceSelect.From.CrossJoin(DLImage.TableFullName);
                existanceSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                query += "IF NOT EXISTS (" + existanceSelect.Statement.Trim().Replace("\n", "\n		") + ")";
                query += "\r\nBEGIN";
                query += "\r\n	" + this.f_InsertBuilder.Statement.Trim().Replace("\n", "\n	");
                query += "\r\n SET @Inserted=1 ";
                query += "\r\nEND";
                query += "\r\nELSE";
                query += "\r\nBEGIN";

                if (this.f_Image_TimeStamp != null)
                {
                    SelectBuilder timeStampSelect = new SelectBuilder();
                    timeStampSelect.Select.Add(this.addPrefix("CurrentTimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                    timeStampSelect.From.CrossJoin(DLImage.TableFullName);
                    timeStampSelect.Where.AND(DLImage.PropertiesMappings.Image_ID + " = " + this.addPrefix("Image_ID"));
                    query += "\r\n" + timeStampSelect.Statement;
                    query += "\r\nIF " + this.addPrefix("CurrentTimeStamp") + " = " + this.addPrefix("Image_TimeStamp");
                    query += "\r\nBEGIN";
                }
                this.FillFilter(this.f_UpdateBuilder.Where, list);
                query += "\r\n" + this.f_UpdateBuilder.Statement;
                query += "\r\n SET @Inserted=0 ";
                if (this.f_Image_TimeStamp != null)
                {
                    query += "\r\nEND";
                }
                query += "\r\nEND";


                SelectBuilder sb = new SelectBuilder();
                sb.From.CrossJoin(DLImage.TableFullName);
                sb.Top = 1;
                sb.Select.Add(this.addPrefix("Image_TimeStamp") + " = " + DLImage.PropertiesMappings.Image_TimeStamp);
                this.FillFilter(sb.Where, null);
                query += "\r\n" + sb.Statement;

                count = DataBaseConnections.CustomersSuppliers.ExecuteNonQuery(query, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray());
                if (this.f_Image_TimeStamp != null && Prm_CurrentTimeStamp.Value != DBNull.Value)
                {
                    byte[] timeStampOldvalues = (byte[])f_Image_TimeStamp;
                    byte[] timeStampNewValues = (byte[])Prm_CurrentTimeStamp.Value; ;
                    for (int i = 0; i < timeStampOldvalues.Length; i++)
                    {
                        if (timeStampOldvalues[i] != timeStampNewValues[i])
                        {
                            this.f_Error = new Exception("Concurrency Problem,TimeStamp Value Changed.");
                            break;
                        }
                    }
                }
                if (this.f_Error == null)
                {
                    Inserted = Convert.ToBoolean(InsertedParameter.Value);
                    this.f_Image_TimeStamp = (Prm_Image_TimeStamp.Value == DBNull.Value ? null : (byte[])Prm_Image_TimeStamp.Value);
                }
            }
            return count;
        }


        /// <summary>
        /// select  records from the Images table.
        /// </summary>
        public DataTable Select()
        {
            DataTable dt = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                dt = DataBaseConnections.CustomersSuppliers.GetDataTable(this.f_SelectBuilder.Statement, CommandType.Text, ref this.f_Error, list.ToArray());
            }
            return dt;
        }


        /// <summary>
        /// select  records from the Images table using Settings.
        /// </summary>
        public DataTable Select(Settings setting)
        {
            DataTable dt = null;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                list.AddRange(setting.Parameters);
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                dt = DataBaseConnections.CustomersSuppliers.GetDataTable(this.f_SelectBuilder.Statement, CommandType.Text, setting.Connection, setting.Command, setting.Adapter, ref this.f_Error, list.ToArray());
            }
            return dt;
        }


        /// <summary>
        /// select  records from the Images table inside the range.
        /// </summary>
        public DLImage[] Select(int? start, int? end)
        {
            List<DLImage> items = new List<DLImage>();
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                try
                {
                    using (DbDataReader reader = DataBaseConnections.CustomersSuppliers.ExcuteReader(this.f_SelectBuilder.Statement, CommandType.Text, ref this.f_Error, list.ToArray()))
                    {
                        int i = 0;
                        if (!start.HasValue)
                        {
                            start = 0;
                        }
                        while (reader.Read())
                        {
                            if (end.HasValue && end < i)
                            {
                                break;
                            }
                            if (start <= i)
                            {
                                items.Add(new DLImage(reader));
                            }
                            i++;
                        }
                        reader.Close();
                        reader.Dispose();
                    }
                }
                catch (Exception er)
                {
                    this.f_Error = er;
                }
            }
            return items.ToArray();
        }

        /// <summary>
        /// select  records from the Images table inside the range using setting.
        /// </summary>
        public DLImage[] Select(int? start, int? end, Settings setting)
        {
            List<DLImage> items = new List<DLImage>();
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                list.AddRange(setting.Parameters);
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                try
                {
                    using (DbDataReader reader = DataBaseConnections.CustomersSuppliers.ExcuteReader(this.f_SelectBuilder.Statement, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray()))
                    {
                        int i = 0;
                        if (!start.HasValue)
                        {
                            start = 0;
                        }
                        while (reader.Read())
                        {
                            if (end.HasValue && end < i)
                            {
                                break;
                            }
                            if (start <= i)
                            {
                                items.Add(new DLImage(reader));
                            }
                            i++;
                        }
                        reader.Close();
                        reader.Dispose();
                    }
                }
                catch (Exception er)
                {
                    this.f_Error = er;
                }
            }
            return items.ToArray();
        }



        /// <summary>
        /// load first record matching current values.
        /// </summary>
        public bool LoadData()
        {
            this.f_Error = null;
            bool isLoaded = false;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                try
                {
                    using (DbDataReader reader = DataBaseConnections.CustomersSuppliers.ExcuteReader(this.f_SelectBuilder.Statement, CommandType.Text, ref this.f_Error, list.ToArray()))
                    {
                        if (reader.Read())
                        {
                            this.ImportFrom(reader);
                            isLoaded = true;
                        }
                        reader.Close();
                        reader.Dispose();
                    }
                }
                catch (Exception er)
                {
                    this.f_Error = er;
                }
            }
            return isLoaded;
        }

        /// <summary>
        /// load first record matching current values using settings.
        /// </summary>
        public bool LoadData(Settings setting)
        {
            this.f_Error = null;
            bool isLoaded = false;
            if (DataBaseConnections.CustomersSuppliers == null)
            {
                this.f_Error = new Exception("DataBaseConnections.CustomersSuppliers Not Initialized");
            }
            else
            {
                List<DbParameter> list = new List<DbParameter>();
                list.AddRange(setting.Parameters);
                this.FillFilter(this.SelectBuilder.Where, list);
                this.FillSort(list);
                try
                {
                    using (DbDataReader reader = DataBaseConnections.CustomersSuppliers.ExcuteReader(this.f_SelectBuilder.Statement, CommandType.Text, setting.Connection, setting.Transaction, setting.Command, ref this.f_Error, list.ToArray()))
                    {
                        if (reader.Read())
                        {
                            this.ImportFrom(reader);
                            isLoaded = true;
                        }
                        reader.Close();
                        reader.Dispose();
                    }
                }
                catch (Exception er)
                {
                    this.f_Error = er;
                }
            }
            return isLoaded;
        }

        /// <summary>
        /// get values from data row
        /// </summary>
        public virtual void ImportFrom(DataRow dr)
        {
            Type t = typeof(DLImage.PropertiesMappings);
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string Column = property.GetValue(null, null).ToString().Substring(DLImage.TableFullName.Length + 1 + CustomersSuppliers.IdentifierPrefix.Length);
                Column = Column.Substring(0, Column.Length - CustomersSuppliers.IdentifierPostfix.Length);
                if (dr.Table.Columns.Contains(Column))
                {
                    this.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).SetValue(this, dr[Column] == DBNull.Value ? null : dr[Column], null);
                }
            }
        }

        /// <summary>
        /// get values from data reader
        /// </summary>
        public virtual void ImportFrom(DbDataReader Reader)
        {
            Type t = typeof(DLImage.PropertiesMappings);
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string Column = property.GetValue(null, null).ToString().Substring(DLImage.TableFullName.Length + 1 + CustomersSuppliers.IdentifierPrefix.Length);
                Column = Column.Substring(0, Column.Length - CustomersSuppliers.IdentifierPostfix.Length);
                try
                {
                    this.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).SetValue(this, Reader[Column] == DBNull.Value ? null : Reader[Column], null);
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// get values from other DLImage
        /// </summary>
        public virtual void ImportFrom(DLImage otherItem)
        {
            Type t = typeof(DLImage.PropertiesMappings);
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object value = otherItem.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(otherItem, null);
                this.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).SetValue(this, value, null);
            }
        }

        /// <summary>
        /// set values to data row
        /// </summary>
        public virtual void ExportTo(DataRow dr)
        {
            Type t = typeof(DLImage.PropertiesMappings);
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string Column = property.GetValue(null, null).ToString().Substring(DLImage.TableFullName.Length + 2);
                Column = Column.Substring(0, Column.Length - 1);
                if (dr.Table.Columns.Contains(Column))
                {
                    if (this.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(this, null) == null)
                    {
                        dr[Column] = DBNull.Value;
                    }
                    else
                    {
                        dr[Column] = this.GetType().GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(this, null);
                    }
                }
            }
        }
        #endregion

        #region class PropertiesMappings
        /// <summary>
        /// get FullQalified Columns Names in DataBase
        /// </summary>            
        public class PropertiesMappings
        {
            public static string Image_ID
            {
                get
                {
                    return DLImage.TableFullName + "." + CustomersSuppliers.IdentifierPrefix + "Image_ID" + CustomersSuppliers.IdentifierPostfix;
                }
            }
            public static string Image_Default
            {
                get
                {
                    return DLImage.TableFullName + "." + CustomersSuppliers.IdentifierPrefix + "Image_Default" + CustomersSuppliers.IdentifierPostfix;
                }
            }
            public static string Image_Data
            {
                get
                {
                    return DLImage.TableFullName + "." + CustomersSuppliers.IdentifierPrefix + "Image_Data" + CustomersSuppliers.IdentifierPostfix;
                }
            }
            public static string Image_Description
            {
                get
                {
                    return DLImage.TableFullName + "." + CustomersSuppliers.IdentifierPrefix + "Image_Description" + CustomersSuppliers.IdentifierPostfix;
                }
            }
            public static string Image_TimeStamp
            {
                get
                {
                    return DLImage.TableFullName + "." + CustomersSuppliers.IdentifierPrefix + "Image_TimeStamp" + CustomersSuppliers.IdentifierPostfix;
                }
            }
            internal static string getColumnName(string property)
            {
                PropertyInfo pi = typeof(DLImage.PropertiesMappings).GetProperty(property, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase);
                return (pi != null ? pi.GetValue(null, null).ToString() : "");
            }
        }
        #endregion

    }
}
   